package com.one.blocks.rbac.convert;

import com.one.blocks.mvc.convert.BaseDomainConvert;
import com.one.blocks.rbac.controller.query.SysRoleQuery;
import com.one.blocks.rbac.controller.vo.SysRoleGrantVo;
import com.one.blocks.rbac.controller.vo.SysRoleVo;
import com.one.blocks.rbac.domain.SysRole;
import com.one.blocks.rbac.domain.SysUserRole;
import com.one.blocks.rbac.util.SubjectHelper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2023-10-25 10:58:07
 */
@Mapper(uses = {TimestampConvert.class}, imports = {SubjectHelper.class})
public interface SysRoleConvert extends BaseDomainConvert<SysRole, SysRoleVo, SysRoleQuery> {

    public static SysRoleConvert INSTANCE = Mappers.getMapper(SysRoleConvert.class);

    default List<SysUserRole> grantVoToGrantEntity(SysRoleGrantVo grantVo) {
        if (CollectionUtils.isEmpty(grantVo.getRoleIds())) {
            return null;
        }
        List<SysUserRole> dataToInsert = new ArrayList<>(grantVo.getRoleIds().size());
        for (Integer userId : grantVo.getRoleIds()) {
            SysUserRole entity = new SysUserRole();

            entity.setRoleId(grantVo.getUserId());
            entity.setUserId(userId);
            entity.setCreateId(SubjectHelper.getSubjectId());
            entity.setCreateTime(SubjectHelper.getAccessTime());
            dataToInsert.add(entity);
        }
        return dataToInsert;
    }
}